package com.study.chapter05.operator.source;

import com.study.entity.Event;
import lombok.SneakyThrows;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.kafka.clients.producer.ProducerConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @Description: flink读取数据的方式
 * @Author: LiuQun
 * @Date: 2022/7/27 21:38
 */
public class ReadDataTest {
    @SneakyThrows
    public static void main(String[] args) {
        //环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //1.从集合中读取数据
        List<Event> dataList1 = new ArrayList<>(2);
        dataList1.add(new Event("Bob","./home",10000L));
        dataList1.add(new Event("Lily","./cart/pro",20000L));
        DataStreamSource<Event> stream1 = env.fromCollection(dataList1);

        //2.从元素中读取数据
        DataStreamSource<Event> stream2 = env.fromElements(
                new Event("Bob", "./home", 10000L),
                new Event("Lily", "./cart/pro", 20000L)
        );

        //3.从文件中
        DataStreamSource<String> stream3 = env.readTextFile("input/cart.txt");

        //4.从socket流中读取
        DataStreamSource<String> stream4 = env.socketTextStream("192.168.200.131",7777);

        // stream1.print("集合");
        // stream2.print("元素");
        // stream3.print("file");
        // stream4.print("socket");

        //5.从kafka中获取数据
        Properties properties = new Properties();
        properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.30.100:9092");
        properties.setProperty("group.id", "consumer-group");
        properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("auto.offset.reset", "latest");
        //创建kafka消费者
        FlinkKafkaConsumer<String> kafkaConsumer = new FlinkKafkaConsumer<>(
                "msb-items",
                new SimpleStringSchema(),
                properties
        );
        DataStreamSource<String> stream5 = env.addSource(kafkaConsumer);

        stream5.print("kafka");

        env.execute();
    }
}
